package Easy;

// 53.最大子序和
public class Solution53 {
    //[-2,1,-3,4,-1,2,1,-5,4]
    //思路：sum基于贪心，看看如果下一个数当做起点，会不会比当前的总和要大，是的话就替换；maxSum则用于保存全局最大
    public int maxSubArray(int[] nums) {
        int sum = nums[0];
        int maxSum = nums[0];
        for (int i = 1; i<nums.length ; i++){
            sum = Math.max(nums[i], sum + nums[i]);
            maxSum = Math.max(maxSum,sum);
        }
        return maxSum;
    }
}
